package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Teacher;
import com.woniuxy.dao.TeacherDao;
import com.woniuxy.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Teacher)表服务实现类
 *
 * @author zjx
 * @since 2021-05-14 19:41:33
 */
@Service("teacherService")
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherDao teacherDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Teacher queryById(Integer id) {
        return this.teacherDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Teacher> queryAllByLimit(int offset, int limit) {
        List<Teacher> teachers = this.teacherDao.queryAllByLimit(offset, limit);
        System.out.println(teachers);
        return teachers;
    }

    /**
     * 新增数据
     *
     * @param teacher 实例对象
     * @return 实例对象
     */
    @Override
    public String insert(Teacher teacher) {

        int row = this.teacherDao.insert(teacher);
        String result = "新增一条讲师信息失败";
        if (row == 1){
            result = "新增一条讲师信息成功";
        }
        return result;
    }

    /**
     * 修改数据
     *
     * @param teacher 实例对象
     * @return 实例对象
     */
    @Override
    public String update(Teacher teacher) {
        int row = this.teacherDao.update(teacher);
        String result = "修改一条讲师信息失败";
        if (row == 1){
            result = "修改一条讲师信息成功";
        }
        return result;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public String deleteById(Integer id) {
        int row = this.teacherDao.deleteById(id);
        String result = "删除一条讲师信息失败";
        if (row == 1){
            result = "删除一条讲师信息成功";
        }
        return result;
    }

    /**
     *
     * @param startPage 起始页码
     * @param pageSize  每页条数
     * @param teacher   讲师信息
     * @return  讲师信息分页集合
     */
    @Override
    public PageInfo<Teacher> queryAllPage(int startPage, int pageSize, Teacher teacher) {
        PageHelper.startPage(startPage,pageSize);
        System.out.println(teacher);
        List<Teacher> teacherList = teacherDao.queryAll(teacher);
        System.out.println(teacherList);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList);
        return pageInfo;
    }

    /**
     * 查询所有标签
     * @return  标签集合
     */
    @Override
    public List<String> selectLabels() {
        List<String> labels =  this.teacherDao.selectLabels();
        return labels;
    }
}
